package groupb6.shopsystem.controller.customer;

import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import groupb6.shopsystem.pojo.entity.Product;
import groupb6.shopsystem.pojo.form.AddCartForm;
import groupb6.shopsystem.service.CustomerProductService;
import groupb6.shopsystem.util.response.UniversalResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "11:商品详情页")
@ApiSupport(author = "zhangyn")
@RestController
@RequestMapping("/customer/product")
public class CustomerProductController {

    @Autowired
    CustomerProductService customerProductService;

    @ApiOperation("根据id查询商品详情")
    @GetMapping("/getById")
    @ApiResponses(value = {
            @ApiResponse(code = 1000, message = "成功"),
            @ApiResponse(code = 1001, message = "不存在该商品"),})
    @ResponseBody
    UniversalResponse<Product> product(@RequestParam Integer id) {
        Product product = customerProductService.getProductDetailById(id);
        if (product != null) {
            return new UniversalResponse().ok(product);
        } else {
            return new UniversalResponse(1001, "不存在该商品");
        }
    }

    @ApiOperation("添加商品进购物车")
    @PostMapping("/addCart")
    @ApiResponses(value = {
            @ApiResponse(code = 1000, message = "成功")})
    @ResponseBody
    UniversalResponse<?> addCart(AddCartForm addCartForm) {
        return customerProductService.addProIntoCart(addCartForm);
    }
}
